package com.sample.javabean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;




import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.GgaoForm;
import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;


/**
 * 公告信息类
 * @author梁桂花
 *
 * 2018-10-27
 */
public class Ggao {
	
	private static Log LOG =LogFactory.getLog(Ggao.class);
	//定义属性
	private Integer id;
	private String	news;
	private String	person	;
	private String	posttime	;
	
	private JdbcUtils jdbcUtils = new JdbcUtils();

	public Ggao(Integer id){
		this.id =id;
	}
	
	
	/**
	 * 公告信息列表
	 * @return
	 */
	public List<Ggao> findAll(){
		Connection conn=null;
		Statement stmt =null;
		ResultSet rs =null;
		List<Ggao> ggaoList =new ArrayList<Ggao>(20);
		try{
			 conn =jdbcUtils.getConn();
			 stmt =conn.createStatement();
			//构造插入sql
			String sql="select * from t_ggao where 1=1";
			 rs =stmt.executeQuery(sql);
			while(rs.next()){
				String news=rs.getString("news");
				String person=rs.getString("person");
			    String	posttime=rs.getString("posttime");
			   
				
				 Ggao model=new Ggao(id,news,person,posttime);
				
				ggaoList.add(model);
							}
			
		}catch (Exception e) {
			e.printStackTrace();
			LOG.error("加载公告失败!", e);
		}finally{
			jdbcUtils.close(conn, stmt,rs);
		}
		return ggaoList;
		
	}
	
     public void load()throws Exception{
 		Connection conn = null;
 		Statement stmt = null;
 		ResultSet rs =null; 
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			
 			String sql = "select * from  t_ggao where id='"+this.id+"'";
 		
 			rs=stmt.executeQuery(sql);
 			if(rs.next()){
 				this.id =rs.getInt("id");
 				this.news =rs.getString("news");
 				this.person =rs.getString("person");
 				this.posttime=rs.getString("posttime");
 			  	
 				
 			}
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 			throw new SQLException("加载公告信息");
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}	
 	}
 	public void editGgao() {

 		Connection conn = null;
 		Statement stmt = null;
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			
 			String sql = "update  t_ggao set news='"+this.news+"',person='"+this.person+"',posttime='"+this.posttime+"' where id='"+this.id+"'";
 			System.out.println("编辑公告信息");
 			stmt.executeUpdate(sql);
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}

 	}
 	public void delGgao() {

 		Connection conn = null;
 		Statement stmt = null;
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			// 3:閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓绲奞L
 			String sql = "delete from t_ggao  where id='"+this.id+"'";
 			// 4:鎵ч敓鏂ゆ嫹SQL閿熸枻鎷烽敓鏂ゆ嫹閿熸埅鏂ゆ嫹閿燂拷
 			System.out.println("删除公告信息");
 			stmt.executeUpdate(sql);
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}

 	}
 	public void add(){
 		System.out.println("增加公告信息");
 		Connection conn =null;
 		Statement stmt=null;
 		try{
 			
 			 conn =jdbcUtils.getConn();
 		
 			String sql="insert into t_ggao(news,person,posttime) values('"+this.news+"','"+this.person+"','"+this.posttime+"')";
 			
 			 stmt =conn.createStatement();
 			stmt.executeUpdate(sql);
 		}catch (Exception e) {
 			e.printStackTrace();
 			
 		}finally{
 			jdbcUtils.close(conn, stmt);
 			
 		}
 		
 	}
 	public Pager<Ggao> list(int page,int size,GgaoForm form) {

 		Connection conn = null;
 		Statement stmt = null;
 		ResultSet rs =null;
 		Pager<Ggao> results=new Pager<Ggao>();
 		results.setPage(page);
 		results.setSize(size);
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			int start=(page-1)*size;
 			int limit=size;
 			//閿熸枻鎷疯SQL
 			String selectSql="select * from t_ggao where 1=1";
 			if(form!=null&&form.getId()!=null&&!"".equals(form.getId())){
 				selectSql +=" and id like'%"+form.getId()+"%'";
 			}
 			
 			//缁熼敓鏂ゆ嫹SQL
 			String countSql ="select count(*)as c from ("+selectSql+") as t1";
 			//閿熸枻鎷烽〉SQL
 			 String pageSql=selectSql+" limit "+start+","+limit;
 			 rs= stmt.executeQuery(countSql);
 			 int total=0;
 			 while(rs.next()){
 				 total=rs.getInt("c");
 			 }
 			 results.setTotal(total);
 			 //閿熸枻鎷疯閿熸枻鎷烽〉閿熸枻鎷锋伅
 			 rs= stmt.executeQuery(pageSql);
 			 while(rs.next()){
 				 int id=rs.getInt("id");
 				String news=rs.getString("news");
				String person =rs.getString("person");
			    String	posttime=rs.getString("posttime");
			   
 				 Ggao model=new Ggao();
 				  model.setId(id);
 				  model.setNews(news);
 				  model.setPerson(person);
 				  model.setPosttime(posttime);
 				 
 		
 				  results.getDataSets().add(model);
 			 }
 			 results.init();
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}
 		return results;

 	}
 	//定义无参构造函数
 		public Ggao(){
 			
 		}

	public Ggao( Integer id, String news, String person, String posttime ) {
		super();
		this.id = id;
		this.news = news;
		this.person=person;
		this.posttime=posttime;
		
	}


	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public String getNews() {
		return news;
	}


	public void setNews(String news) {
		this.news = news;
	}


	public String getPerson() {
		return person;
	}


	public void setPerson(String person) {
		this.person = person;
	}


	public String getPosttime() {
		return posttime;
	}


	public void setPosttime(String posttime) {
		this.posttime = posttime;
	}


	public JdbcUtils getJdbcUtils() {
		return jdbcUtils;
	}


	public void setJdbcUtils(JdbcUtils jdbcUtils) {
		this.jdbcUtils = jdbcUtils;
	}


	


	
	
}
